﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace XDesigner.Development.Dom
{
    internal static class SharpDevelopHelper
    {
        public static bool LoadCmbx(SolutionInfo sln, string fileName)
        {
            sln.FileName = fileName;
            sln.Name = Path.GetFileNameWithoutExtension(fileName);
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.Load(fileName);
            sln.Version = xmlDoc.DocumentElement.GetAttribute("fileversion");
            if (xmlDoc.DocumentElement.HasAttribute("name"))
            {
                sln.Name = xmlDoc.DocumentElement.GetAttribute("name");
            }
            sln.Style = SolutionStyle.SharpDevelop;
            LoadSubCmbx(sln, fileName);
            return true;
        }

        private static void LoadSubCmbx(SolutionInfo sln , string fileName )
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(fileName);
            string rootPath = Path.GetDirectoryName(fileName);
            string slnRootPath = Path.GetDirectoryName(sln.FileName);
            foreach (XmlElement element in xmlDoc.SelectNodes("Combine/Entries/Entry"))
            {
                string fn = element.GetAttribute("filename");
                if (fn.StartsWith(".\\"))
                {
                    fn = fn.Substring(2);
                    fn = Path.Combine(rootPath, fn);
                }
                if (fn.EndsWith(".cmbx", StringComparison.CurrentCultureIgnoreCase))
                {
                    LoadSubCmbx(sln , fn );
                    SourceFileInfo file = new SourceFileInfo();
                    file.FullFileName = fn;
                    if (fn.StartsWith(slnRootPath, StringComparison.CurrentCultureIgnoreCase))
                    {
                        fn = fn.Substring(slnRootPath.Length);
                        if (fn.StartsWith("\\"))
                        {
                            fn = fn.Substring(1);
                        }
                        file.FileName = fn;
                    }
                    else
                    {
                        file.FileName = fn;
                    }
                    sln.Files.Add(file);
                }
                else if (fn.EndsWith(".prjx", StringComparison.CurrentCultureIgnoreCase))
                {
                    ProjectInfo prj = new ProjectInfo();
                    prj.Style = ProjectStyle.CSSharpDevelop11;
                    LoadFromPrjx(prj, fn);
                    sln.Projects.Add(prj);
                }
            }//foreach
        }
        public static bool LoadFromPrjx( ProjectInfo prj , string strFileName)
        {
            prj.Files.Clear();
            string xml = FileHelper.LoadAnsiFile(strFileName);
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xml);
            prj.ProjectFileName = strFileName;
            prj.RootPath = System.IO.Path.GetDirectoryName(strFileName);
            prj.Name = doc.DocumentElement.GetAttribute("name");
            if (doc.DocumentElement.GetAttribute("projecttype") == "C#")
            {
                prj.Style = ProjectStyle.CSSharpDevelop11;
            }
            //SourceFileInfo ProjFile = new SourceFileInfo();
            //ProjFile.FileName = System.IO.Path.GetFileName(strFileName);
            //ProjFile.FullFileName = strFileName;
            //ProjFile.Style = FileStyle.None;
            //prj.Files.Add(ProjFile);
            foreach (System.Xml.XmlElement element in doc.SelectNodes("Project/Contents/File"))
            {
                SourceFileInfo NewFile = new SourceFileInfo();
                string file = element.GetAttribute("name");
                if (file.StartsWith(".\\"))
                {
                    file = file.Substring(2);
                }
                NewFile.FileName = file;
                NewFile.FullFileName = System.IO.Path.Combine(prj.RootPath, file);
                if (element.GetAttribute("subtype") == "Directory")
                {
                    NewFile.Style = FileStyle.Directory;
                }
                else if (element.GetAttribute("buildaction") == "EmbedAsResource")
                {
                    NewFile.Style = FileStyle.Resource;
                }
                else if (element.GetAttribute("buildaction") == "Compile")
                {
                    NewFile.Style = FileStyle.SourceCode;
                }
                else
                {
                    NewFile.Style = FileStyle.None;
                }
                prj.Files.Add(NewFile);
            }//foreach

            return true;
        }
    }
}
